<template>
  <el-card style="width: 500px;margin: auto; text-align: center">
    <el-form :model="item" >
      <el-form-item>
        <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model='item.dsUserName'  ></el-input>

      </el-form-item>
      <el-form-item label="手机号" :label-width="formLabelWidth">
        <el-input v-model='item.dsUserTel'></el-input>
      </el-form-item>
      <el-form-item label="邮箱" :label-width="formLabelWidth" >
        <el-input v-model="item.dsUserEmail"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="update()">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "User",
  data() {
    return {
      item: {
        dsUserName:null,
        dsUserEmail: null,
        dsUserTel:null,
        description: null,
      },
      formLabelWidth: '120px',
      imageUrl: '',
    }
  },
  methods: {
    update() {
      this.api.userApi.updateMine(this.item)
      .then(res => {
        if (res.code === "200") {
          let user = JSON.parse(localStorage.getItem('user'))
          user.username = res.data.dsUserName
          localStorage.setItem('user', JSON.stringify(user));
          this.$message.success("修改成功")
        }
      })
      .catch(err => {
        this.$message.error("未知错误")
        console.log(err)
      })
    },

    selectUserByName() {
      this.api.userApi.selectUserByName()
      .then(res => {
        if (res.code === "200") {
          this.item = res.data
        }
      }).catch(err => {
        this.$message.error("未知错误")
        console.log(err)

      })
    },

    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },

  },
  mounted() {
    this.selectUserByName()
  }
}
</script>


<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>
